package com.dianrui.agent.activity;

import android.os.Bundle;
import android.widget.TextView;

import com.dianrui.agent.R;
import com.dianrui.agent.okhttp.OkHttpRequest;
import com.dianrui.agent.util.SharedUtil;
import com.dianrui.agent.util.StringUtils;
import com.dianrui.agent.util.ToastUtil;
import com.dianrui.agent.util.Url;
import com.google.gson.JsonObject;


import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

public class MessageDetails extends BaseActivity{
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.art_title)
    TextView artTitle;
    @BindView(R.id.art_time)
    TextView artTime;
    @BindView(R.id.art_content)
    TextView artContent;
    private String mMessageId;

    @Override
    public int getContentViewId() {
        return R.layout.message_details;
    }

    @Override
    public void initView() {
        if (getIntent()!= null && getIntent().getExtras()!=null) {

            Bundle bundle = getIntent().getExtras();
            if (bundle != null) {
                mMessageId=bundle.getString("id");
            }
        }
        title.setText("消息详情");
        requestMessageDetails();

    }


    private void requestMessageDetails() {
        JsonObject jsonObject=new JsonObject();
        jsonObject.addProperty("messages_id",mMessageId);
        OkHttpRequest.getInstance().postTokenRequests(SharedUtil.getUrl()+Url.MESSAGEINFO, jsonObject.toString(),  new OkHttpRequest.XCallBack() {
            @Override
            public void onResponse(String response) {
                if (!StringUtils.isEmpty(response)) {
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        if ("1000".equals(jsonObject.optString("status"))) {
                            artTitle.setText(jsonObject.optJSONObject("data").optString("title"));
                            artTime.setText(jsonObject.optJSONObject("data").optString("create_time"));
                            artContent.setText(jsonObject.optJSONObject("data").optString("content"));
                        }else if ("1001".equals(jsonObject.optString("status"))){
                            ToastUtil.showToast(jsonObject.optString("message"));
                        }else if ("1002".equals(jsonObject.optString("status")))  {
                            spUtils.clear();
                            ToastUtil.showToast(jsonObject.optString("message"));
                            JumpActivitys(LoginActivity.class);
                            finish();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }else {
                    ToastUtil.showToast(getString(R.string.net_error));
                }
            }

            @Override
            public void FailedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }
}
